import sqlite3

# 检查数据库结构
def check_database_structure():
    try:
        # 连接较大的数据库文件
        db_path = 'trade_monitor.db'
        print(f"正在检查数据库文件: {db_path}")
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        # 检查所有表
        cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
        tables = cursor.fetchall()
        
        print("=== 数据库表结构检查 ===")
        print(f"数据库中的表数量: {len(tables)}")
        
        for table in tables:
            table_name = table[0]
            print(f"\n表名: {table_name}")
            
            # 获取表结构
            cursor.execute(f"PRAGMA table_info({table_name});")
            columns = cursor.fetchall()
            
            print("列信息:")
            for column in columns:
                cid, name, type, notnull, dflt_value, pk = column
                pk_status = "主键" if pk else "非主键"
                notnull_status = "NOT NULL" if notnull else "NULL"
                default_value = f"默认值: {dflt_value}" if dflt_value else "无默认值"
                print(f"  - {name} ({type}, {notnull_status}, {pk_status}, {default_value})")
            
            # 如果是trade_records表，获取一些记录示例
            if table_name == 'trade_records':
                cursor.execute(f"SELECT * FROM {table_name} LIMIT 3;")
                records = cursor.fetchall()
                
                print(f"\n{table_name}表的前3条记录:")
                for record in records:
                    print(f"  {record}")
        
    except Exception as e:
        print(f"检查数据库结构时出错: {str(e)}")
    finally:
        if conn:
            conn.close()

if __name__ == "__main__":
    check_database_structure()